package ru.nsu.effel.actions;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import ru.nsu.effel.persistence.ConnectionBid;
import ru.nsu.effel.persistence.ConnectionBidManager;
import ru.nsu.effel.persistence.Tariff;
import ru.nsu.effel.persistence.TariffManager;

public class EmployeeProfileActionBean implements ActionBean {
	private ActionBeanContext context;
	
	public ActionBeanContext getContext() {
		return context;
	}

	public void setContext(ActionBeanContext context) {
		this.context = context;
	}
	
	public List<ConnectionBidPrintHelper> getFitterBids() {
		HttpSession session = getContext().getRequest().getSession(false);
		Integer accountId = (Integer) session.getAttribute("accountId");
		List<ConnectionBid> bids = ConnectionBidManager.getInstance().getFitterConnectionBids(accountId);
		if (null == bids) return null;
		List<ConnectionBidPrintHelper> info = new LinkedList<ConnectionBidPrintHelper>();
		for (ConnectionBid connectionBid : bids) {
			String str = TariffManager.getInstance().getById(connectionBid.getTariffId()).getName();
			ConnectionBidPrintHelper helper = new ConnectionBidPrintHelper(connectionBid.getId(), str, connectionBid.getFirstName(), connectionBid.getLastName(), connectionBid.getAddress());
			info.add(helper);
		}
		return info;
	}
	
	public List<ConnectionBidPrintHelper> getNewConnectionBids() {
		List<ConnectionBidPrintHelper> helpers = ConnectionBidManager.getInstance().getConnectionBidIdTariff();
		return helpers;
	}
	
	public List<ConnectionBidPrintHelper> getManagerConnectionBids() {
		String status = null;
		HttpSession session = getContext().getRequest().getSession(false);
		Integer accountId = (Integer)session.getAttribute("accountId");
		List<ConnectionBid> bids = ConnectionBidManager.getInstance().getManagerConnectionBids(accountId);
		if (null == bids) return null;
		List<ConnectionBidPrintHelper> helpers = new LinkedList<ConnectionBidPrintHelper>();
		for (ConnectionBid connectionBid : bids) {
			Tariff tariff = TariffManager.getInstance().getById(connectionBid.getTariffId());
			if (1 == connectionBid.getBidStatus()) {
				status = "Wait fot fitter";
			} else if (2 == connectionBid.getBidStatus()){
				status = "In progress";
			} else if (3 == connectionBid.getBidStatus()) {
				status = "Done";
			}
			helpers.add(new ConnectionBidPrintHelper(connectionBid.getId(), tariff.getName(), connectionBid.getFirstName(), connectionBid.getLastName(), connectionBid.getAddress(), status));
		}
		return helpers;
	}
}
